﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.CrmContacto.Code;
using CrmContacto.Contracts;
using CrmContacto.Contracts.Contracts.Search;
using Web.Framework;
using CrmContacto.WebApp.Code.InnerClasses;
using Web.Framework.Translate;
using CrmContacto.Contracts.Contracts;
using Web.CrmContacto.Code.Security;

namespace CrmContacto.WebApp.App
{
    public partial class AccionDeMarketingPromocionReglaEdit : BasePage
    {
        public AccionDeMarketingPromocionRegla reglaCurrent
        {
            get
            {
                return (AccionDeMarketingPromocionRegla)ViewState["AccionDeMarketingPromocionRegla"];
            }
            set
            {
                ViewState["AccionDeMarketingPromocionRegla"] = value;
            }
        }

        private List<LinkButton> actionButtons = new List<LinkButton>();
        LinkButton eliminarButton = new LinkButton();
        LinkButton guardarButton = new LinkButton();
        LinkButton volverButton = new LinkButton();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            
            guardarButton.ID = "BoCommandGuardar";
            guardarButton.Text = LocalizationProvider.Instance["Bo.Command.Guardar"];
            guardarButton.Click += new EventHandler(lnkGuardar_Click);
            actionButtons.Add(guardarButton);
        
            SetJavascriptForClientClickDirtyChech(volverButton);
            volverButton.ID = "BoCommandVolver";
            volverButton.CausesValidation = false;
            volverButton.Text = LocalizationProvider.Instance["Bo.Command.Volver"];
            SetJavascriptForClientClickDirtyChech(volverButton);
            volverButton.Click += new EventHandler(lnkCancelar_Click);
            actionButtons.Add(volverButton);
            
            eliminarButton.ID = "BoCommandEliminar";
            eliminarButton.CausesValidation = false;
            eliminarButton.Text = LocalizationProvider.Instance["Bo.Command.Eliminar"];
            eliminarButton.Click += new EventHandler(eliminarButton_Click);
            actionButtons.Add(eliminarButton);

            base.LoadContextActions(actionButtons);
        }

        private void eliminarButton_Click(object sender, EventArgs e)
        {
            try
            {
                WebServiceHelper.EliminarRegla(reglaCurrent.Id);
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlReglaList + string.Format("?{0}=1", MiscClass.QueryShowDeleted), true);
                //NuevaRegla();
                //Bind();
                //this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Message.RegistroEliminado"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            try
            {
                HasPermissionToSeeMe(CrmSecurity.MANAGE_PROMO_RULES);

                if (!this.IsPostBack)
                {
                    if (this.Request.QueryString["Id"] == null)
                    {
                        NuevaRegla();
                    }
                    else
                    {
                        MaestroBaseSearch search = new MaestroBaseSearch();
                        search.EmpresaId = BaseWebPage.CompanyId;
                        search.Id = Convert.ToInt32(this.Request.QueryString["Id"]);
                        reglaCurrent = WebServiceHelper.GetReglasAccionDeMarketing(search).First();
                    }

                    Bind();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void NuevaRegla()
        {
            reglaCurrent = new AccionDeMarketingPromocionRegla();
            reglaCurrent.EmpresaId = BasePage.CompanyId;
        }

        protected override void ApplySecurity()
        {
            base.ApplySecurity();

            eliminarButton.Visible = reglaCurrent.Id != 0;

            if (reglaCurrent.Id != 0)
            {
                SetJavascriptForDeleteRecord(eliminarButton, reglaCurrent.Nombre);
            }

            //this.lnkGuardar.Visible = HasPermissionFor(PlanProdSecurity.CAUSADESVIO_EDITAR);
        }

        private void Bind()
        {
            if (reglaCurrent.Id != 0)
            {
                this.txtNombre.Text = reglaCurrent.Nombre;
                this.txtDescripcion.Text = reglaCurrent.Descripcion;
            }
        }

        private void SetData()
        {
            reglaCurrent.Nombre = this.txtNombre.Text;
            reglaCurrent.Descripcion = this.txtDescripcion.Text;
        }

        private void Save()
        {
            reglaCurrent = WebServiceHelper.SaveReglasAccionDeMarketing(reglaCurrent);
        }

        private void Validar()
        {
            // TODO
        }

        protected void lnkGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                Validar();
                SetData();
                Save();
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlReglaList + string.Format("?{0}=1", MiscClass.QueryShowSaveOk), true);
                //Bind();
                //this.masterPage.ShowMessage(LocalizationProvider.Instance["Bo.Messages.MensajeSaveExitoso"], WebMessageType.Notification);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkCancelar_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlReglaList, true);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}